package com.rds.common.core.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.rds.common.core.domain.entity.DynamicVo;
import com.rds.common.core.mapper.DynamicMapper;
import com.rds.common.core.service.IDynamicService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Service业务层处理
 *
 * @author libin
 */
@Service
public class DynamicServiceImpl implements IDynamicService
{
    @Autowired
    private DynamicMapper dynamicMapper;

    /**
     * 查询列表
     * @return 集合
     */
    @Override
    public List<HashMap<String,Object>> dynamicSelect(DynamicVo dynamicVo)
    {
        return dynamicMapper.dynamicSelect(dynamicVo);
    }

    /**
     * 查询列表
     * @return 集合
     */
    @Override
    public List<HashMap<String,Object>> selectList(DynamicVo dynamicVo)
    {
        return dynamicMapper.selectList(dynamicVo);
    }

    /***
     * 查询列表数据
     * @param tableName 表名
     * @param queryWrapper 查询条件
     * @return 对象集合
     */
    @Override
    public List<HashMap<String,Object>> selectDataList(String tableName,ArrayList<String> queryWrapper) {
        DynamicVo docDynamicVo=new DynamicVo();
        docDynamicVo.setTableName(tableName);
        docDynamicVo.setQueryWrapper(queryWrapper);
        docDynamicVo.setOrderBy(" create_time desc");
        return dynamicMapper.selectList(docDynamicVo);
    }

    /**
     * 查询列表
     * @return 集合
     */
    @Override
    public List<HashMap<String,Object>> selectListByIds(String tableName,Long[] ids)
    {
        return dynamicMapper.selectListByIds(tableName,ids);
    }

    /**
     * 根据id查询一条数据
     * @return 数据
     */
    @Override
    public HashMap<String,Object> selectDataById(String tableName,Long id)
    {
        DynamicVo dynamicVo=new DynamicVo();
        dynamicVo.setTableName(tableName);
        dynamicVo.setId(id);
        return dynamicMapper.selectDataById(dynamicVo);
    }

    /**
     * 根据参数查询数据
     * @return 数据
     */
    public HashMap<String,Object> selectDataByDynamic(String tableName,ArrayList<String> queryWrapper,String selectColumn){
        DynamicVo dynamicVo=new DynamicVo();
        dynamicVo.setTableName(tableName);
        dynamicVo.setQueryWrapper(queryWrapper);
        return dynamicMapper.selectDataByDynamic(dynamicVo);
    }

    /**
     * 查询一条数据
     * @return 数据
     */
    @Override
    public HashMap<String,Object> selectOne(DynamicVo dynamicVo)
    {
        return dynamicMapper.selectOne(dynamicVo);
    }

    /**
     * 查询连表数据列表
     * @return 集合
     */
    @Override
    public List<HashMap<String,Object>> selectListJoin(DynamicVo dynamicVo)
    {
        return dynamicMapper.selectListJoin(dynamicVo);
    }

    /**
     * 查询递归数据列表
     * @return 集合
     */
    @Override
    public List<HashMap<String,Object>> selectRecursionList(DynamicVo dynamicVo)
    {
        return dynamicMapper.selectRecursionList(dynamicVo);
    }

    /**
     * 新增数据
     * @return 数据
     */
    @Override
    public int insertData(String tableName,Map<String, Object> data)
    {
        return dynamicMapper.insertData(tableName,data);
    }

    /**
     * 新增数据
     * @return 数据
     */
    @Override
    public int insertBatchData(String tableName,List<Map<String, Object>> data)
    {
        return dynamicMapper.insertBatchData(tableName,data);
    }

    /**
     * 修改数据
     * @return 数据
     */
    @Override
    public int updateData(String tableName, Map<String, Object> data, ArrayList<String> wrapper)
    {
        return dynamicMapper.updateData(tableName,data,wrapper);
    }

    /**
     * 删除数据
     * @return 数据
     */
    @Override
    public int deleteData(String tableName,ArrayList<String> wrapper)
    {
        return dynamicMapper.deleteData(tableName,wrapper);
    }

    /**
     * 根据ID删除数据
     * @return 数据
     */
    @Override
    public int deleteDataById(String tableName,Long id){
        return dynamicMapper.deleteDataById(tableName,id);
    }

    /**
     * 根据ID删除数据
     * @return 数据
     */
    @Override
    public int deleteDataByIds(String tableName,Long[] ids){
        return dynamicMapper.deleteDataByIds(tableName,ids);
    }

    /**
     * 处理json对象
     * @return 数据
     */
    public List<Map<String, Object>> getJsonToMap(JSONObject object)
    {
        Set<Map.Entry<String, Object>> entrySet=object.entrySet();
        List<Map<String, Object>> list=new ArrayList<>();
        for (Map.Entry<String, Object> entry:entrySet){
            Map<String,Object> map=new HashMap<>();
            map.put("key",entry.getKey());
            map.put("value",entry.getValue());
            list.add(map);
        }
        return list;
    }

    /**
     * 处理数据-由Map转成List<Map>
     * @return 数据
     */
    public List<Map<String, Object>> getMapToList(Map<String, Object> data)
    {
        Set<Map.Entry<String, Object>> entrySet=data.entrySet();
        List<Map<String, Object>> list=new ArrayList<>();
        for (Map.Entry<String, Object> entry:entrySet){
            Map<String,Object> map=new HashMap<>();
            map.put("key",entry.getKey());
            map.put("value",entry.getValue());
            list.add(map);
        }
        return list;
    }
}
